473,461 Members | 1,413 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to encode text into html format

Hi,

I want to encode input text into html format such as replace "<" with "&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred


Jun 27 '08 #1
10 2912
Sam
Fred Yu writes:
Hi,

I want to encode input text into html format such as replace "<" with "&lt",
That's "&lt;".
replace "&" with "&amp".
That's "&amp;".
Could you give me some ideas? Thanks.
Try to do your homework assignment by yourself. This is a simple
search/replace operation, and there are many ways to get it done.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)

iEYEABECAAYFAkhC4aQACgkQx9p3GYHlUOJbHQCdFkadJpXTIS tsphfd9cZ1yjhc
4QwAniVg+D//DKToDNz651FJF5MCYIlo
=xhO4
-----END PGP SIGNATURE-----

Jun 27 '08 #2
Fred Yu wrote:
Hi,

I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.

Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform
Best

Kai-Uwe Bux
Jun 27 '08 #3
On Jun 1, 12:37*pm, "Fred Yu" <jean_y_f...@sohu.comwrote:
Hi,

I want to encode input text into html format such as replace "<" with "&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred
google iconv. It will convert from many char encodings to many other
char
encodings. I've used it to "format" text in various XML wrapper
classes.
Jun 27 '08 #4

"Fred Yu" <je*********@sohu.comschrieb im Newsbeitrag
news:g1**********@news.cn99.com...
Hi,

I want to encode input text into html format such as replace "<" with
"&lt",
replace "&" with "&amp".
Example for AnsiString Class

AnsiString Input; //contains the html code
int pos;

do // replace "<" to "&lt"
{
if(Input.Pos("<") NULL)
{
pos = Input.Pos("<");
Input.Delete(pos,1);
Input.Insert("%26lt",pos);
}
}
while(Input.Pos("<") NULL);

Jun 27 '08 #5
On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
Fred Yu wrote:
I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.
Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform
Agreed for the first (although it may be overkill---in this
particular case, I think I'd go with a simple switch).

No real need for the second; just use istream::get() and
ostream::put() (or operator<< in some cases).

As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #6
James Kanze wrote:
On Jun 1, 8:11 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>Fred Yu wrote:
I want to encode input text into html format such as replace "<" with
"&lt", replace "&" with "&amp".
Could you give me some ideas? Thanks.
>Containers: std::map< char, std::string >
Iterators: std::istream_iterator, std::ostream_iterator
Algorithms: std::transform

Agreed for the first (although it may be overkill---in this
particular case, I think I'd go with a simple switch).

No real need for the second; just use istream::get() and
ostream::put() (or operator<< in some cases).

As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
I was thinking of something like this:

#include <iostream>
#include <iterator>
#include <map>
#include <algorithm>
#include <cassert>

struct encoder {

std::map< char, std::string the_map;

encoder ( void ) {
the_map[ 'a' ] = "a";
// ...
the_map[ '&' ] = "&amp";
// ...
}

std::string const & operator() ( char ch ) const {
std::map< char, std::string >::const_iterator iter =
the_map.find( ch );
assert( iter != the_map.end() );
return ( iter->second );
}
};

int main ( void ) {
encoder the_encoder;
std::transform( std::istreambuf_iterator<char>( std::cin ),
std::istreambuf_iterator<char>(),
std::ostream_iterator<std::string>( std::cout, "" ),
the_encoder );
}
Best

Kai-Uwe Bux

Jun 27 '08 #7
Hi!

James Kanze schrieb:
As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
The source range of transform may have another value type than the
destination range.

char const* replace(char);

transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);

Frank
Jun 27 '08 #8
On Jun 1, 11:25 pm, Frank Birbacher <bloodymir.c...@gmx.netwrote:
James Kanze schrieb:
As to the third: how? You're replacing a single character with
a sequence of characters, and transform does a one to one (which
in practice makes it of fairly limited utility---although I've
used it with a vector<string>, ostream_iterator, and as string
transformer class that I've written, which works something like
$(patsubst...) in GNU make).
The source range of transform may have another value type than the
destination range.
I'm aware of that, however...
char const* replace(char);
transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);
For some reason, I was thinking in terms of std::string, and not
char const*. And converting each std::string seemed a bit heavy
for the task at hand. But a statically generated char const*[];
why not?

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9
Hi!

James Kanze schrieb:
>char const* replace(char);
>transform(str.begin(), str.end(),
ostream_iterator<const char*>(cout),
&replace);

For some reason, I was thinking in terms of std::string, and not
char const*. And converting each std::string seemed a bit heavy
for the task at hand. But a statically generated char const*[];
why not?
Yes. I think I needed such a conversion once and used a switch. The
obvious problem is to efficiently handle a char that is not transformed
to more than one char (the common case). I think I actually used
for_each instead of transform:

void appendReplacement(ostream& stream, const char c)
{
switch(c)
{
case '<': stream << "&lt;"; break;
default: stream << c; break;
}
}

This makes it possible to append different types (char or char*) to the
stream and yet requires no [CHAR_MAX] array, but lets the compiler
choose the most efficient lookup (through the switch).

Of course can this function be implemented as a functor.

Frank
Jun 27 '08 #10

"Fred Yu" <je*********@sohu.comдÈëÏûÏ¢ÐÂÎÅ:g1**********@new s.cn99.com...
Hi,

I want to encode input text into html format such as replace "<" with
"&lt",
replace "&" with "&amp".
Could you give me some ideas? Thanks.

Fred

Thanks for your help.
Jun 27 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: francescomoi | last post by:
Hi. I'm trying to store a text within a MySQL field (v 3.23.58) by using MySQLdb (v 1.2.1c3). The text is: "telephone..." (note the last character) And I get this error message:...
3
by: user | last post by:
hi there has anyone of you writte a function to encode html from like '&' -> '&amp;' and likes to share it with me.. or can anybody give me a hint how to set up something like that. cheers me. ...
5
by: Scott Matthews | last post by:
I've recently come upon an odd Javascript (and/or browser) behavior, and after hunting around the Web I still can't seem to find an answer. Specifically, I have noticed that the Javascript...
4
by: Newbie | last post by:
How would I modify this form to encode *all* the characters in the 'source' textarea to the '%xx' format & place result code into the 'output' textarea? (cross browser compatable) Any help is...
2
by: ViperDK | last post by:
What is the best way for that? I store all Data in the original form in the Database. To prevent output fields (especially the fields everyone can use) to do bad things like killing the...
4
by: Darrel | last post by:
How does HTML.encode work? I'm trying to save text in a hidden form field into a SQL DB. The tedt is HTML (from a WYSIWYG editor...X-standard). One problem I have is that stray apostrophe's in...
7
by: jtfaulk | last post by:
I need to encode some information on the server side using ASP.NET with C#; sending via HTTP to a client side application, that needs to be decoded in an MFC C++ application. I'm not sure if I...
0
by: younus | last post by:
Hi Guys I need some help i want to encode arabic text to UCS2 format in asp my text بيسليسلسيس is converted to 0633062C064A06440643002006280646062C0627 this format plese help me this is very...
0
by: younus | last post by:
Hi Guys I need some help i want to encode arabic text to UCS2 format in asp my text بيسليسلسيس is converted to 0633062C064A06440643002006280646062C0627 this format plese help me this is very...
9
by: Fred Yu | last post by:
Hi, I want to encode input text into html format such as replace "<" with "&lt", replace "&" with "&amp". Could you give me some ideas? Thanks. Fred
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.